﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

//using PowerCreator.LMS.Logic.Common;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_PaperManager_TestPaperUserList : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    Department opDepart = new Department();

    const string SEARCH_PAPER_TUSER_USERPAPERHISTORY = "select * from VM_UserTestPaperList where 1=1 and FKUPHistoryCourseID<>0  {0}";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            BinderDepartmentClass();
            //ViewState["ProjectID"] = Base64.Decode(Request.QueryString["p"].ToString());
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);

            InitPage();
            //GridBand("", -1, -1, int.Parse(ViewState["ProjectID"].ToString()));

        }
    }

    private void BinderDepartmentClass()
    {
        string search = "DepartmentType = 2";
        DataTable dt = opDepart.Query(search);

        dpdlst_ClassInfo.DataSource = dt;
        dpdlst_ClassInfo.DataTextField = "DepartmentName";
        dpdlst_ClassInfo.DataValueField = "DepartmentId";
        dpdlst_ClassInfo.DataBind();

        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        dpdlst_ClassInfo.Items.Insert(0, li);

        dpdlst_ReadState.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_ReadState.Items[1].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_ReadState.Items[2].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_TestPaperUserList_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_UserName.Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_ReadState.Text = GetLanageValueByKey("lab_PaperUserList_ReadState", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_UserClass.Text = GetLanageValueByKey("lbl_AddStudent_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_RealName.Text = GetLanageValueByKey("lbl_StudentManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperName.Text = GetLanageValueByKey("ProjectResourceSet_lbl_l6", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Course_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Course_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private string GetSearch()
    {

        string realName = txt_RealName.Text.Trim();
        string username = txtUserName.Text.Trim();
        int readstate = int.Parse(dpdlst_ReadState.SelectedValue);
        StringBuilder search = new StringBuilder();

        if (ViewState["SearchType"].ToString() == "0")
        {
            if (txt_SearchKey.Text.Trim() != "")
            {
                search.Append(string.Format(" and UserName like '%{0}%' or RealName like '%{0}%' or PaperName like '%{0}%'",txt_SearchKey.Text.Trim()));
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(username))
            {
                search.Append(" and UserName like '%" + username + "%'");
            }
            if (readstate != -1)
            {
                search.Append(" and UPHistoryReadState='" + readstate + "'");
            }
            if (!string.IsNullOrEmpty(realName))
            {
                search.Append(" and RealName like '%" + realName + "%'");
            }
            if (dpdlst_ClassInfo.SelectedValue!= "0")
            {
                search.Append(" and DepartmentId=" + dpdlst_ClassInfo.SelectedValue);
            }
            if (txt_PaperName.Text.Trim() != "")
            {
                search.Append(" and PaperName like '%" + txt_PaperName.Text.Trim()+ "%'");
            }
        }
        string strSearch = string.Format(SEARCH_PAPER_TUSER_USERPAPERHISTORY, search);
        return strSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        //DataTable dt = new DataTable();
        //dt = objViewQuery.Query(search, FBasePage.PageSize(), pageIndex);
        //dt.Columns.Add("url");
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    dt.Rows[i]["url"] = "PaperID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryPaperID"].ToString()) + "&UserID=" + Base64.Encode(dt.Rows[i]["UserID"].ToString()) + "&ProjectID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryProjectID"].ToString()) + "&ClassID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryClassID"].ToString()) + "&CourseID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryCourseID"].ToString()) + "&ProjectResourceID=" + Base64.Encode(dt.Rows[i]["UserID"].ToString());
        //}
        //gvPaperUserList.DataSource = dt;
        //gvPaperUserList.DataBind();

        DataTable dt = new DataTable();
        dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        dt.Columns.Add("url");
        dt.Columns.Add("ProjectResourceID");
        dt.Columns.Add("CourseResourceID");

        PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
        PowerCreator.LMS.Web.Course.Course objCourse = new PowerCreator.LMS.Web.Course.Course();




        for (int i = 0; i < dt.Rows.Count; i++)
        {

            
            #region 获取方案资源ID和课程资源ID
            /*
            if (dt.Rows[i]["FKUPHistoryCourseID"].ToString().Trim() == "0")//0:试卷，1：课程底下的试卷
            {
                string search_ProjectResource = " ProresClass=2 and FKProresTrainproID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKProresPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString();
                DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);
                if (dt_ProjectResource.Rows.Count == 0)
                {
                    dt.Rows[i]["ProjectResourceID"] = 0;
                }
                else
                {
                    dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
                }
                dt.Rows[i]["CourseResourceID"] = "0";
            }
            else
            {
                string search_ProjectResource = " ProresClass=2 and FKProresTrainproID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + " and FKProresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString();//+ " and FKProresPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString()
                DataTable dt_ProjectResource = objProjectResource.QueryProjectResource(search_ProjectResource);

                if (dt_ProjectResource.Rows.Count == 0)
                {
                    dt.Rows[i]["ProjectResourceID"] = 0;
                }
                else
                {
                    dt.Rows[i]["ProjectResourceID"] = dt_ProjectResource.Rows[0]["ProresID"].ToString();
                }

                string search_CourseResource = " fkCoursersResourceID=0 and CoursersClass=2 and FKCourseresCourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + " and FKCoursersPaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString();

                DataTable dt_CourseResource = objCourse.QueryCourseResource(search_CourseResource);

                if (dt_CourseResource.Rows.Count == 0)
                {
                    dt.Rows[i]["CourseResourceID"] = 0;
                }
                else
                {
                    dt.Rows[i]["CourseResourceID"] = dt_ProjectResource.Rows[0]["CourseresID"].ToString();
                }

            }  */
            #endregion


            dt.Rows[i]["url"] = "PaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString() + "&ProjectID=0" + "&ClassID=0" + "&UserID=" + dt.Rows[i]["FKUPHistoryUserID"].ToString() + "&CourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString() + "&ProjectResourceID=0" +  "&CourseResourceID=0" ;
            //dt.Rows[i]["url"] = "PaperID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryPaperID"].ToString()) + "&ProjectID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryProjectID"].ToString()) + "&ClassID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryClassID"].ToString()) + "&UserID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryUserID"].ToString()) + "&CourseID=" + Base64.Encode(dt.Rows[i]["FKUPHistoryCourseID"].ToString()) + "&ProjectResourceID=" + Base64.Encode(dt.Rows[i]["ProjectResourceID"].ToString()) + "&CourseResourceID=" + Base64.Encode(dt.Rows[i]["CourseResourceID"].ToString());
        }
        gvPaperUserList.DataSource = dt;
        gvPaperUserList.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //搜索
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);

    }

    protected void gvPaperUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string url = string.Empty;
        if (e.CommandName == "readpaper")
        {
            url = "ReadTestPaper.aspx?" + e.CommandArgument.ToString();

        }
        else if (e.CommandName == "studyhis")
        {
            url = "ReadedPaper.aspx?" + e.CommandArgument.ToString();

        }
        else
        {
            url = "EditUserTestPaper.aspx?" + e.CommandArgument.ToString();

        }
        Script.RedirectURL(url);
    }
    protected void gvPaperUserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[1].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[2].Text = GetLanageValueByKey("msg_ProjectClassUser_SubmitTime", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[3].Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[4].Text = GetLanageValueByKey("lab_EditUserPaper_RealScore", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[5].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanshijian", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[6].Text = GetLanageValueByKey("lbl_StudentManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[7].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

            //e.Row.Cells[8].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanren", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");

            LinkButton btn = new LinkButton();
            btn = (LinkButton)e.Row.Cells[6].FindControl("btn_readpaper");
            LinkButton btn_Edit = (LinkButton)e.Row.Cells[6].FindControl("btn_Edit");
            LinkButton btn_studyhis = (LinkButton)e.Row.Cells[6].FindControl("btn_studyhis");

           btn_studyhis.Text=GetLanageValueByKey("btn_PaperList_xiangqing", PowerCreator.LMS.Language.LanagetItemType.Page);

            btn.Text = GetLanageValueByKey("btn_ProjectClassUser_ReadPaper", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_Edit.Text = GetLanageValueByKey("btn_ProjectClassUser_UpdatePaper", PowerCreator.LMS.Language.LanagetItemType.Page);

            if (e.Row.Cells[3].Text.Trim() == "0" | e.Row.Cells[3].Text.Trim() == "&nbsp;")
            {
                e.Row.Cells[3].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_Edit.Enabled = false;
                btn.Enabled = true;
                btn_studyhis.Enabled = false;


            }
            else
            {
                e.Row.Cells[3].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn_Edit.Enabled = true;
                btn.Enabled = false;
                btn_studyhis.Enabled = true;
            }
            if (e.Row.Cells[2].Text.Trim() == "&nbsp;")
            {
                e.Row.Cells[2].Text = GetLanageValueByKey("msg_ProjectClassUser_NoAccomplish", PowerCreator.LMS.Language.LanagetItemType.Page);
                btn.Enabled = false;
            }
        }


    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
}
